import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { createLoginComponent } from 'yklib'
import { YkImageDisplay as Canvas } from 'yklib'
import { YkVideo as Video } from 'yklib'
// import { TempPage } from 'yklib'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Login',
    component: () => Promise.resolve(createLoginComponent({serverHost: 'https://sges.yangke.site', serverPort: '5001'}))
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('./components/ges/SgesCalA/SgesCalA.vue')
  },
  {
    path: '/SgesCalA',
    name: 'SgesCalA',
    component: () => import('./components/ges/SgesCalA/SgesCalA.vue')
  },
  {
    path: '/SgesCalC',
    name: 'SgesCalC',
    component: () => import('./components/ges/SgesCalC/SgesC.vue')
  },
  {
    path: '/MountainGesCalA',
    name: 'MountainGesCalA',
    component: () => import('./components/ges/MountainA/MountainGesCalA.vue')
  },
  {
    path: '/Uncertainty',
    name: 'Uncertainty',
    component: () => import('./components/uncertainty/Uncertainty.vue')
  },
  {
    path: '/ExcelViewer',
    name: 'ExcelViewer',
    component: () => import('./components/common/viewer.vue')
  },
  {
    path: '/EbsFlow',
    name: 'EbsFlow',
    component: () => import('./components/ebs/useEbsFlow.vue')
  },
  {
    path: '/Canvas',
    name: 'Canvas',
    component: Canvas
  },
  {
    path: '/Video',
    name: 'Video',
    component: Video
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('./views/AboutView.vue')
  },
  {
    path: '/debug',
    name: 'debug',
    component: () => import('./views/DebugView.vue')
  },
  {
    path: '/PricePower',
    name: 'pricePower',
    component: () => import('./components/PriceInfo.vue')
  }
]


export const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('authToken') // 假设我们使用localStorage存储认证令牌
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    // 如果目标路由需要登录且用户未登录，则重定向到登录页面
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 可以将当前路由的路径作为参数传递给登录页面，以便登录后重定向回来
    })
  } else {
    // 否则，继续导航
    next()
  }
})


// export default router;